package com.hnyfkj.jyindustry.common.config;

import com.hnyfkj.jyindustry.common.utils.RedisObjectSerializer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * @program: jyindustry
 * @description:redis配置类
 * @author: zl
 * @create: 2020-07-09 18:05
 **/

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

	@Override
	@Bean
	public KeyGenerator keyGenerator() {
		return (target, method, params) -> {
			final StringBuilder sb = new StringBuilder();
			sb.append(target.getClass().getName());
			sb.append(method.getName());
			for (final Object obj : params) {
				sb.append(obj.toString());
			}
			return sb.toString();
		};
	}

	// @Bean
	// public CacheManager cacheManager(final RedisConnectionFactory
	// connectionFactory) {
	// final RedisCacheManager redisCacheManager =
	// RedisCacheManager.create(connectionFactory);
	// return redisCacheManager;
	// }

	@Bean
	public CacheManager cacheManager(final RedisConnectionFactory connectionFactory) {
		// 初始化一个RedisCacheWriter
		final RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
		// 设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key，JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现
		// ClassLoader loader = this.getClass().getClassLoader();
		// JdkSerializationRedisSerializer jdkSerializer = new
		// JdkSerializationRedisSerializer(loader);
		// RedisSerializationContext.SerializationPair<Object> pair =
		// RedisSerializationContext.SerializationPair.fromSerializer(jdkSerializer);
		// RedisCacheConfiguration
		// defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
		final RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
		// 设置默认超过期时间是30秒
		defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
		// 初始化RedisCacheManager
		final RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
		return cacheManager;
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate(final RedisConnectionFactory factory) {
		final RedisTemplate<String, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(factory);
		template.setKeySerializer(new StringRedisSerializer());
		template.setValueSerializer(new RedisObjectSerializer());
		return template;
	}

	// @Bean
	// public RedisTemplate<String, String> redisTemplate(final
	// RedisConnectionFactory factory) {
	// final StringRedisTemplate template = new StringRedisTemplate(factory);
	// final Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
	// Jackson2JsonRedisSerializer(Object.class);
	// final ObjectMapper om = new ObjectMapper();
	// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	// jackson2JsonRedisSerializer.setObjectMapper(om);
	// template.setValueSerializer(jackson2JsonRedisSerializer);
	// template.afterPropertiesSet();
	// return template;
	// }

}
